package com.example.lenovo.myapplication;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_linearlayout);
        ImageView message_imv = (ImageView)findViewById(R.id.message);
        ImageView list_imv = (ImageView)findViewById(R.id.list);
        ImageView search_imv = (ImageView)findViewById(R.id.search);
        ImageView me_imv  = (ImageView)findViewById(R.id.me);
        message_imv.setOnClickListener(this);
        list_imv.setOnClickListener(this);
        search_imv.setOnClickListener(this);
        me_imv.setOnClickListener(this);
        replaceFragment(new MessageFragment());

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.message:
                replaceFragment(new MessageFragment());
                break;
            case R.id.list:
                replaceFragment(new ListFragment());
                break;
            case R.id.search:
                replaceFragment(new SearchFragment());
                break;
            case R.id.me:
                replaceFragment(new MeFragment());
                break;
            default:
                break;
        }


    }
    private void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.main_layout,fragment);
        transaction.commit();
    }
}
